
package com.zntg.api.contract;
import com.google.gson.Gson;
import com.zntg.api.contract.param.ContractSyncToCrmParam;
import com.zntg.api.renGouShu.enmus.ResultStatus;
import com.zntg.api.renGouShu.result.ResultModel;
import com.zntg.api.shuanglu.enums.ShuangLuResultStatus;
import com.zntg.api.shuanglu.param.DoubleViedoGetParam;
import com.zntg.api.shuanglu.param.DoubleViedoUpdateParam;
import com.zntg.api.shuanglu.result.DoubleViedoResult;
import com.zntg.api.shuanglu.result.DownloadFileResult;
import com.zntg.contants.ShuangLuApiContants;
import com.zntg.entity.TblobstorageNewEntity;
import com.zntg.mapper.aiiidb.DoubleViedoAcceDtlMapper;
import com.zntg.mapper.crmdb.DoubleViedoAcceCrmMapper;
import com.zntg.mapper.crmdb.TblobStorageDoubleViedoMapper;
import com.zntg.mapper.crmdb.TblobtSorageMapper;
import com.zntg.utils.DateUtil;
import com.zntg.utils.DownloadPdf;
import com.zntg.utils.FileUtils;
import com.zntg.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 文件同步api接口，将app附件同步到crm下面
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = ShuangLuApiContants.NAMESPACE,tags =  ShuangLuApiContants.NAMESPACE )
@RestController
@RequestMapping("/api/v2/file")
public class FileSyncApiController {
    @Value("${template.targetPath}")
    String targetPath;
    @Autowired
    private Environment env;
    @Autowired
    private DoubleViedoAcceDtlMapper doubleViedoAcceDtlMapper;
    @Autowired
    private DoubleViedoAcceCrmMapper doubleViedoAcceCrmMapper;
    @Autowired
    private TblobtSorageMapper tblobtSorageMapper;
    @Autowired
    private TblobStorageDoubleViedoMapper tblobStorageDoubleViedoMapper;
    @PostMapping(value = "/syncToCrm",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "app附件同步到crm",notes = "app附件同步到crm",position = 1,response = Object.class)
    @ResponseBody
    public Object crmSync(ContractSyncToCrmParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            Gson gson=new Gson();
            System.out.println("app附件自动同步到crm开始,参数"+gson.toJson(param).toString());
                String url=param.getFileUrl();
                //获取crmAcceId
                String crmAcceId=param.getlStorageId();
                //下载远程双录文件到本地
                String fileSize="";
                String today= DateUtil.getToDay();
                String fileSaveName=crmAcceId+"baccessory";
                String filePath=targetPath+today+"/Tblobstorage/"+crmAcceId.substring(0,crmAcceId.length()-1)+"/";
                System.out.println("文件路径========"+filePath);
                File file=new File(filePath+fileSaveName);
                if(file.exists()){
                    System.out.println("文件已存在，即将删除。。。。。。。。。。。。");
                    file.delete();
                }else{
                    System.out.println("文件不存在，即将创建。。。。。。。。。。。。");
                }
                   FileUtils.createFile(filePath+fileSaveName);
                   DownloadFileResult downloadFileResult= DownloadPdf.downLoadByUrl(url,
                           fileSaveName,filePath);
            System.out.println("文件创建成功。。。。。。。。。。。。");
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }
    /**
    * @Description: 参数校验
    * @Author: zhengyunfei
    * @Date: 2019/6/24
    * @time:13:43
    */
    private boolean validParam(ResultModel resultModel, DoubleViedoGetParam param) {
        String reserveId=param.getReserveId();//预约编号
        if(StringUtil.isNullOrEmpty(reserveId)){
            resultModel.setCode(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getCode());
            resultModel.setMessage(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getMessage());
            return true;
        }
        String htNo=param.getHtNo();//合同号
        if(StringUtil.isNullOrEmpty(htNo)){
            resultModel.setCode(ShuangLuResultStatus.HT_NO_NOTNULL.getCode());
            resultModel.setMessage(ShuangLuResultStatus.HT_NO_NOTNULL.getMessage());
            return true;
        }
        String userId=param.getUserId();//员工编号
        if(StringUtil.isNullOrEmpty(userId)){
            resultModel.setCode(ShuangLuResultStatus.USER_NO_NOTNULL.getCode());
            resultModel.setMessage(ShuangLuResultStatus.USER_NO_NOTNULL.getMessage());
            return true;
        }
        return false;
    }



}
